from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from .serializers import AreaSerializer, SubAreaSerializer


class AreaViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    # 不分页
    pagination_class = None

    def get_queryset(self):
        if self.action == 'list':
            province = Area.objects.filter(parent=None)
            return province
        else:
            return Area.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer
